package com.zzjson.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateFormatUtil {
	
	public static final String DATE_COMPACT_FORMAT = "yyyyMMdd";
	public static final String DATETIME_ISO_FORMAT = "yyyy-MM-dd HH:mm:ss";
	public static final String DATETIME_EN_US_FORMAT = "MM/dd/yyyy HH:mm:ss";
	public static final String DATETIME_ISO_FORMAT_T = "yyyy-MM-dd'T'HH:mm:ss";
	public static final String DATETIME_ISO_FORMAT_WITHOUT_SPACE = "yyyy-MM-ddHH:mm:ss";
	public static final String DATETIME_COMPACT_FORMAT = "yyyyMMddHHmmss";
	public static final String DATE_SLASH_FORMAT = "yyyy/MM/dd";
	public static final String DATE_LINE_FORMAT = "yyyy-MM-dd";
	public static final String DATETIME_MIN_FORMAT = "yyyy-MM-dd HH:mm";
	public static final String DATETIME_ONLY_TIME_PART = "HH:mm:ss";
	public static final String DATETIME_ISO_FORMAT_MILLI = "yyyy-MM-dd HH:mm:ss.ssssss";
	
    public static String generateFormatedDate() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }
    
    public static String generateFormatedDateStringFromDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_ISO_FORMAT);
        return sdf.format(date);
    }
    
    /**
	 * 将日期对象转化为指定格式dateFormat的字符串
	 * @param date
	 * @param dateFormat
	 * @return
	 */
	public static String getDate(Date date,String dateFormat) {
		SimpleDateFormat format = new SimpleDateFormat(dateFormat);
		return format.format(date);
	}
	
	/**
	 * 将指定格式fromFormat的日期字符串转化为日期对象
	 * @param date
	 * @param fromFormat
	 * @return
	 * @throws ParseException
	 */
	public static Date getDate(String date,String fromFormat) throws ParseException{
		SimpleDateFormat fromSimpleDateFormat = new SimpleDateFormat(fromFormat);
		return fromSimpleDateFormat.parse(date);
	}
	
	/**
	 * 将指定日期格式的字符串格式从fromFormat转换为toFormat
	 * @param date
	 * @param fromFormat
	 * @param toFormat
	 * @return
	 * @throws ParseException
	 */
	public static String getDate(String date,String fromFormat,String toFormat) throws ParseException{
		Date fromDate=getDate(date, fromFormat);
		return new SimpleDateFormat(toFormat).format(fromDate);
	}
	/**
	 * 获取当前日期  yyyyMMdd
	 * @return
	 */
	public static final String getCurrentDate(){
	    Calendar calendar = Calendar.getInstance();
	    int i = calendar.get(1);
	    int j = calendar.get(2) + 1;
	    int k = calendar.get(5);
	    return "" + i + (j >= 10 ? "" + j : "0" + j) + (k >= 10 ? "" + k : "0" + k);
	}
	
	/**
	 * 获取当前时间 HHmmss
	 * @return
	 */
	public static final String getCurrentTime(){
        Calendar calendar = Calendar.getInstance();
        int i = calendar.get(11);
        int j = calendar.get(12);
        int k = calendar.get(13);
        return "" + (i >= 10 ? "" + i : "0" + i) + (j >= 10 ? "" + j : "0" + j) + (k >= 10 ? "" + k : "0" + k);
    }
	/**
	 * 把String类型的日期转换为Calendar，用于日期比较
	 * @param s
	 * @return
	 */
	public static Calendar bocmDateToCal(String s)
    {
        int i = Integer.parseInt(s.substring(0, 4));
        int j = Integer.parseInt(s.substring(4, 6)) - 1;
        int k = Integer.parseInt(s.substring(6, 8));
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(i, j, k, 0, 0, 0);
        return calendar;
    }
	
	/**
	 * 获得星期  星期一返回1，星期天返回7
	 * @param date
	 * @return 1-7
	 * */
    public static final int getWeek(String date) {
    	int week = 0;
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(format.parse(date));
		} catch (Exception e) {
			return week;
		}
		week = cal.get(Calendar.DAY_OF_WEEK);
		week = (week - 1 + 7) % 7;
		return week;
    }
    
    /**
	 * 一天的开始时间  xxxx-xx-xx 00:00:00
	 * @param date  当前时间
	 * @return
	 */
	public static Date getDayBegin(Date time) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(time);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return cal.getTime();
	}
	
	public static Date getDayBegin() {
		return getDayBegin(new Date());
	}
	
	/**
	 * 一天的结束时间  xxxx-xx-xx 23:59:59
	 * @param time
	 * @return
	 */
	public static Date getDayEnd(Date time) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(time);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		return cal.getTime();
	}
	
	public static Date getDayEnd() {
		return getDayEnd(new Date());
	}
	
    /**
     * 月初 xxxx-xx-01 00:00:00
     * @return
     */
    public static final Date getMonthBegin(Date date) {
    	Calendar c = Calendar.getInstance();
    	c.setTime(date);
    	c.set(Calendar.DAY_OF_MONTH, 1);
    	c.set(Calendar.HOUR_OF_DAY, 0);
    	c.set(Calendar.MINUTE, 0);
    	c.set(Calendar.SECOND, 0);
    	return c.getTime();
    }
    
    public static final Date getMonthBegin() {
        return getMonthBegin(new Date());	
    }
    
    /**
     * 月末 xxxx-xx-xx 23:59:59
     * @return
     */
    public static final Date getMonthEnd(Date date) {
    	Calendar c = Calendar.getInstance();
    	c.setTime(getMonthBegin(date));
    	c.add(Calendar.MONTH, 1);
    	c.add(Calendar.SECOND, -1);
    	return c.getTime();
    }
    
    public static final Date getMonthEnd() {
    	return getMonthEnd(new Date());
    }
    
    /**
     * 
     * @return
     */
    public static final int getYear(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.YEAR);
    }
    
    /**
     * 
     * @return
     */
    public static final int getMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.MONTH);
    }
    
    /**
     * 
     * @return
     */
    public static final int getDayOfMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.DAY_OF_MONTH);
    }
    
    /**
     * 年初 xxxx-01-01 00:00:00
     * @return
     */
    public static final Date getYearBegin(Date date) {
       Calendar c = Calendar.getInstance();
       c.setTime(date);
       c.set(Calendar.MONTH, 0);
       c.set(Calendar.DAY_OF_MONTH, 1);
       c.set(Calendar.HOUR_OF_DAY, 0);
   	   c.set(Calendar.MINUTE, 0);
   	   c.set(Calendar.SECOND, 0);
       return c.getTime();	
    }
    
    public static final Date getYearBegin() {
       return getYearBegin(new Date());	
    }
    
    /**
     * 年末 xxxx-12-31 23:59:59
     * @return
     */
    public static final Date getYearEnd(Date date) {
    	Calendar c = Calendar.getInstance();
    	c.setTime(getYearBegin(date));
    	c.add(Calendar.YEAR, 1);
    	c.add(Calendar.SECOND, -1);
    	return c.getTime();
    }
    
    public static final Date getYearEnd() {
    	return getYearEnd(new Date());
    }
    
    /**
     * 时间所处月的天数
     * @param date
     * @return
     */
    public static final int getMonthDays(Date date) {
    	Calendar c = Calendar.getInstance();
    	c.setTime(date);
    	c.set(Calendar.DATE, 1);
    	c.roll(Calendar.DATE, -1);
    	return c.get(Calendar.DATE);
    }
    
    public static final int getMonthDays() {
    	return getMonthDays(new Date());
    }
    
    /**
     * 前一天同一时间点
     * @param date
     * @return
     */
    public static final Date getPreviousDay(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DATE, -1);
		return c.getTime();
	}
	
    /**
     * 前一个月同一时间点
     * @param date
     * @return
     */
	public static final Date getPrevicesMonth(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MONTH, -1);
		return c.getTime();
	}
	
	/**
	 * 前一年同一时间点
	 * @param date
	 * @return
	 */
	public static final Date getPreviousYear(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.YEAR, -1);
		return c.getTime();
	}
	
	public static final Date getNextHours(Date date, int hour) {
	    Calendar c = Calendar.getInstance();
	    c.setTime(date);
	    c.add(Calendar.HOUR_OF_DAY, hour);
	    return c.getTime();
	}
	
	public static final Date getDate(int year, int month, int date) {
		Calendar c = Calendar.getInstance();
		c.set(year, month, date);
		return c.getTime();
	}
	
	public static final Date getDateVariant(String year, String month, String date) {
		return getDate(Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(date));
	}
	
	public static int getDaysOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }
	
	public static Date getMidnightDate(int year, int month, int date) throws ParseException {
	    return getDate(year + "-" + month + "-" + date + " 00:00:00", DATETIME_ISO_FORMAT);
	}
	
}
